unit untRewards;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls, ExtCtrls;

type

  { TfrmRewards }

  TfrmRewards = class(TForm)
    btnSave: TButton;
    btnLoad: TButton;
    dlgOpen: TOpenDialog;
    pnlSpacer1: TPanel;
    pnlSPacer: TPanel;
    pnlSpacer2: TPanel;
    pnlSpacer3: TPanel;
    pnlSpacer4: TPanel;
    pnlSpacer5: TPanel;
    pnlSPacer6: TPanel;
    pnlWarning: TPanel;
    pnlRewards: TPanel;
    pnlConsequences: TPanel;
    pnlC1: TPanel;
    pnlC2: TPanel;
    pnlC3: TPanel;
    pnlC4: TPanel;
    dlgSave: TSaveDialog;
    procedure btnLoadClick(Sender: TObject);
    procedure btnSaveClick(Sender: TObject);
    procedure FormDragOver(Sender, Source: TObject; X, Y: Integer;State: TDragState; var Accept: Boolean);
  private
    { private declarations }
  public
    { public declarations }
    Group       : String;
    procedure CreateLabels;
    procedure ClearLabels(NewGroup : String);
  end; 

var
  frmRewards: TfrmRewards;

implementation
Uses
   DataModule;

   procedure TfrmRewards.ClearLabels(NewGroup : String);
   var i, comps : integer;
   begin
        if NewGroup <> Group then
        begin
             Group := NewGroup;
             i:=0;
             try
                Comps := Self.ComponentCount;
                while i < Comps do
                begin
                     if Self.ComponentCount > i then
                     begin
                          if (Self.Components[i] is TLabel) then
                          begin
                               Self.Components[i].Destroy;
                               Dec(Comps);
                          end
                          else
                              inc(i);
                     end;
                 end;
            Finally
              Self.Refresh;
            end;
        end;
   end;

  procedure TfrmRewards.FormDragOver(Sender, Source: TObject; X, Y: Integer;
    State: TDragState; var Accept: Boolean);
  begin
       Accept := (Source is TLabel);
       if accept then
       begin
            TLabel(source).Left:=x;
            TLabel(source).Top :=y;
       end;
  end;

  procedure TfrmRewards.btnSaveClick(Sender: TObject);
  var i : Integer;
      fileline : TStringList;
      FileHandle : TextFile;
  begin
     dlgSave.FileName:='Rewards'+datamodule1.Group;
     if dlgSave.Execute then
     begin
          AssignFile(FileHandle, dlgsave.FileName);
          Rewrite(FileHandle);

          Fileline := TStringList.Create;
          Fileline.Delimiter:=',';
          writeln(FileHandle,Group);
          for i:= 1 to Self.ComponentCount - 1 do
          begin
               if (Self.Components[i] is TLabel) then
               begin
                    fileline.Add(TLabel(Self.Components[i]).Caption);
                    fileline.Add(IntToStr(TLabel(Self.Components[i]).Top));
                    fileline.Add(IntToStr(TLabel(Self.Components[i]).Left));
                    //save to file
                    writeln(FileHandle,fileline.DelimitedText);
                    //clear
                    fileline.Clear;
               end;
          end;
          FileLine.Destroy;
          CloseFile(FileHandle);
     end;
  end;

  procedure TfrmRewards.btnLoadClick(Sender: TObject);
  var
      fileline : TStringList;
      FileHandle : TextFile;
      NewLabel   : TLabel;
      Buffer     : String;
  begin
       if dlgOpen.Execute then
       begin
          ClearLabels('');
          AssignFile(FileHandle, dlgOpen.FileName);
          Reset(FileHandle);
          Readln(FileHandle, Group);
          Fileline := TStringList.Create;
          Fileline.Delimiter:=',';
          while not eof(FileHandle) do
          begin
               Readln(FileHandle,Buffer);
               FileLine.DelimitedText:=Buffer;
               NewLabel := TLabel.Create(self);
               NewLabel.DragMode  := dmAutomatic;
               NewLabel.DragCursor:= crHandPoint;
               NewLabel.Parent    := Self;
               NewLabel.Font.Size := 18;
               NewLabel.Caption   := FileLine[0];
               NewLabel.Top       := StrToInt(FileLine[1]);
               NewLabel.Left      := StrToInt(FileLine[2]);
          end;
          FileLine.Destroy;
          CloseFile(FileHandle);
       end;
  end;

  procedure TfrmRewards.CreateLabels;
var
   NewLabel   : TLabel;
   i,col,down : Integer;
   size       : Integer;
begin
    size := 0;
    col  := 0;
    down := 0;
    for i := 0 to DataModule1.FirstNames.Count-1 do
    begin
        NewLabel := TLabel.Create(self);
        NewLabel.DragMode  := dmAutomatic;
        NewLabel.DragCursor:= crHandPoint;
        NewLabel.Parent    := Self;
        NewLabel.Top       := 390 + (Down * 30);
        NewLabel.Left      := size + (col*100);
        NewLabel.Font.Size := 18;
        NewLabel.Caption   := DataModule1.FirstNames.Strings[i];
        size               := Length(NewLabel.Caption) * 3;
        NewLabel.Tag := 9;
        NewLabel.Show;
        inc(col);
        if col > 7 then
        begin
             col  := 0;
             inc(down);
             size := 0;
        end;
    end;
end;


initialization
  {$I untrewards.lrs}

end.

